package com.nulldev.util.graphics.renderIt.renderer.swing.nitrous2;

import java.awt.peer.ComponentPeer;

import com.nulldev.util.graphics.renderIt.renderer.swing.nitrous2.renderers.D3DRenderer;
import com.nulldev.util.graphics.renderIt.renderer.swing.nitrous2.renderers.GDIRenderer;
import com.nulldev.util.graphics.renderIt.renderer.swing.nitrous2.renderers.GLXRenderer;
import com.nulldev.util.graphics.renderIt.renderer.swing.nitrous2.renderers.WGLRenderer;
import com.nulldev.util.graphics.renderIt.renderer.swing.nitrous2.renderers.X11GLRenderer;
import com.nulldev.util.graphics.renderIt.renderer.swing.nitrous2.renderers.X11Renderer;
import com.nulldev.util.gui.swingAPI.Swing;
import com.nulldev.util.osAPI.OS;

public class Renderers {

	public static Renderer get(final ComponentPeer peer) throws ClassNotFoundException {
		final boolean allowGL = System.getProperty("sun.java2d.opengl", "false").equalsIgnoreCase("true") || OS.isWindows();
		final boolean allowD3D = System.getProperty("sun.java2d.d3d", "true").equalsIgnoreCase("true")
				&& System.getenv().getOrDefault("J2D_D3D_NO_HWCHECK", "false").equalsIgnoreCase("true");
		if (OS.isWindows()) {
			if (allowD3D && D3DRenderer.isSupported())
				return new D3DRenderer(peer);
			if (allowGL && WGLRenderer.isSupported())
				return new WGLRenderer(peer);
			if (!GDIRenderer.isSupported())
				throw new UnsupportedOperationException("Nitrous doesn't support your environment.");
			return new GDIRenderer(peer);
		} else if (OS.isLinux() || OS.isBSD()) {
			if (allowGL && GLXRenderer.isSupported())
				return new GLXRenderer(peer);
			if (Swing.isX11Safe() && allowGL && X11GLRenderer.isSupported())
				return new X11GLRenderer(peer);
			if (Swing.isX11Safe() && X11Renderer.isSupported())
				return new X11Renderer(peer);
			throw new UnsupportedOperationException("Nitrous doesn't support your environment.");
		} else {
			throw new UnsupportedOperationException("Nitrous doesn't support your environment.");
		}
	}
}
